package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.AddressModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/10/21
 */
public class UpdateAddressPresenter implements UpdateAddressModel {
    private final String TAG = getClass().getSimpleName();
    private UpdateAddressView updateAddressView;
    private Context mContext;

    public UpdateAddressPresenter(UpdateAddressView updateAddressView, Context mContext) {
        this.updateAddressView = updateAddressView;
        this.mContext = mContext;
    }


    @Override
    public void updateAddress(String type, String address_id, String uname, String mobile, String province, String city, String town, String street) {
        HttpParams params = new HttpParams();
        params.put("type", type);
        params.put("address_id", address_id);
        params.put("uname", uname);
        params.put("mobile", mobile);
        params.put("province", province);
        params.put("city", city);
        params.put("town", town);
        params.put("street", street);
        OkHttpUtils.post(HttpUtil.EDIT_ADDRESS)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, AddressModel.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        updateAddressView.updateAddressSuccess((AddressModel) data);
                        updateAddressView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        updateAddressView.hideLoading();
                        updateAddressView.httpStatusError();
                    }
                });
    }
}
